Find the sum of
the squares of two numbers.
Input. Two
integers a and b, each of which does not exceed 109 in absolute value.
Output. Print the value
of the expression a2 + b2.
Sample
input |
Sample output |
3 2 |
13 |
elementary problem
Algorithm analysis
Since a, b ≤ 109, then a2 + b2
≤ 2 * 1018. Therefore, to compute the result, the long long type should be
used.
Algorithm implementation
Read the input data.
scanf("%lld %lld",&a,&b);
Compute and print the
answer.
res = a * a + b
* b;
printf("%lld\n",res);
Algorithm implementation – STL
#include <iostream>
using namespace
std;
long long
res, a, b;
int main(void)
{
cin >> a >> b;
res = a * a + b * b;
cout << res << endl;
return 0;
}
Java implementation
import java.util.*;
public class Main
{
public static void
main(String []args)
{
Scanner con = new
Scanner(System.in);
long a = con.nextLong();
long b = con.nextLong();
long res = a*a + b*b;
System.out.println(res);
con.close();
}
}
Python implementation
Read the input data.
a, b = map(int,input().split())
Compute and print the
answer.
res = a**2 + b**2
print(res)
Go implementation
package main
import "fmt"
func main() {
var a, b, res int64
fmt.Scanf("%d %d", &a,&b)
res
= a * a + b * b
fmt.Println(res)
}